// 导入数据库模块
const db = require('../DB/index');
// 导入获取时间的包
const date = require('silly-datetime')

// 获取所有乡镇信息
exports.getRegion = (req,res)=>{
    const sql = `SELECT * FROM region`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取乡镇成功',
            data:result
        })
    })
}

// 获取所有区域信息
exports.getTown = (req,res)=>{
    const sql = `SELECT * FROM town`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取用户成功',
            data:result
        })
    })
}

// 新增乡镇
exports.addRegion=(req,res)=>{
    const obj = req.body
    const sql = `SELECT * FROM region WHERE region_name = '${obj.name}'`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.length > 0) return res.cc('该乡镇已经存在')
        const createDate = date.format(new Date(),'YYYY-MM-DD HH:mm:ss')
        const sql = `INSERT INTO region(region_name,createTime) VALUES ('${obj.name}','${createDate}')`
        db.query(sql,(err,result)=>{
            if(err) return res.cc(err)
            if(result.affectedRows !== 1) return res.cc('新增失败')
            res.cc('新增成功',0)
        })
    })
}

// 修改乡镇
exports.updateRegion = (req,res)=>{
    const obj = req.body
    const sql = `UPDATE region SET region_name = '${obj.region_name}' WHERE  id='${obj.id}'`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('修改失败')
        res.cc('修改成功',0)
    })
}

// 删除乡镇
exports.delRegion = (req,res)=>{
    const obj = req.body
    const sql = `DELETE FROM region WHERE id = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('删除失败')
        res.cc('删除成功',0)
    })
}

// 根据parentId获取区域信息
exports.idToTown = (req,res)=>{
    const obj = req.body
    const sql = `SELECT * FROM town WHERE parentId = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取信息成功',
            data:result
        })
    })
}
// 根据Id获取区域信息
exports.idTown = (req,res)=>{
    const obj =req.body
    const sql = `SELECT * FROM town WHERE id = ${obj.town_id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取信息成功',
            data:result[0]
        })
    })
}


// 新增区域
exports.addTown = (req,res)=>{
    const obj = req.body
    const sql = `SELECT town_name FROM town WHERE town_name = '${obj.town_name}'`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.length > 0) res.cc('区域名称已存在')
        const createDate = date.format(new Date(),'YYYY-MM-DD HH:mm:ss')
        const sql = `INSERT INTO town (parentId,town_name,town_address,town_range,longitude,latitude,createTime) 
            VALUES(${obj.parentId},'${obj.town_name}','${obj.town_address}','${obj.town_range}',${obj.longitude},${obj.latitude},'${createDate}')`
        db.query(sql,(err,result)=>{
            if(err) return res.cc(err)
            if(result.affectedRows !== 1) return res.cc('新增失败')
            res.cc('新增成功',0)
        })
    })
}

// 修改区域
exports.updateTown = (req,res)=>{
    const obj = req.body
    const sql = `UPDATE town SET town_name = '${obj.town_name}',town_address = '${obj.town_address}',
    town_range = '${obj.town_range}',longitude = ${obj.longitude},latitude = ${obj.latitude},parentId = ${obj.parentId} WHERE id = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('修改失败')
        res.cc('修改成功',0)
    })
}

// 删除区域
exports.delTown = (req,res)=>{
    const obj = req.body
    const sql = `DELETE FROM town WHERE id = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('删除失败')
        res.cc('删除成功',0)
    })
}

